package com.tyj.movie.config.interceptor;

import com.alibaba.fastjson.JSON;
import com.tyj.movie.constant.AjaxConstant;
import com.tyj.movie.entity.Users;
import com.tyj.movie.util.R;
import com.tyj.movie.util.SecurityUtil;
import org.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 日期: 2021/7/10 18:48
 * 作者: 谭友杰
 * 类说明:
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Users user = SecurityUtil.getSessionUser();
        if (user==null){
            //为登录返回信息
            response.setContentType("application/json; charset=utf-8");
            PrintWriter writer = response.getWriter();
            R error = R.error(AjaxConstant.AJAX_USER_NO_LOGIN.getCode(), AjaxConstant.AJAX_USER_NO_LOGIN.getMsg());
            writer.println(JSON.toJSON(error));
            return false;
        }else {
            //登录了放行
            return true;
        }

    }
}
